package petstore.persistence;

import petstore.persistence.DBUtil;
import petstore.domain.Log;
import petstore.persistence.impl.LogDao;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class LogDaoImpl implements LogDao {
    private static final String INSERT_LASTTIME = "INSERT INTO web_log (username,lastTime,cat,dog,bird,pokemon,bugCat) VALUES (?,?,?,?,?,?,?)";
    private static final String UPDATE_LASTTIME = "UPDATE web_log SET username = ?,lastTime = ?,cat = ?,dog = ?,bird = ?, pokemon = ?, bugCat = ? WHERE lastTime = ?";

    @Override
    public boolean InsertLastTime(Log log) {
        boolean success = false;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_LASTTIME);
            preparedStatement.setString(1,log.getUsername());
            preparedStatement.setString(2,log.getLastTime());
            preparedStatement.setInt(3,log.getCat());
            preparedStatement.setInt(4,log.getDog());
            preparedStatement.setInt(5,log.getBird());
            preparedStatement.setInt(6,log.getPokemon());
            preparedStatement.setInt(7,log.getBugCat());
            preparedStatement.executeUpdate();
            success = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        if(success) System.out.println("添加成功");
        return success;
    }

    @Override
    public void UpdateLastTime(Log log,String lastTime) {
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_LASTTIME);
            preparedStatement.setString(1,log.getUsername());
            preparedStatement.setString(2,log.getLastTime());
            preparedStatement.setInt(3,log.getCat());
            preparedStatement.setInt(4,log.getDog());
            preparedStatement.setInt(5,log.getBird());
            preparedStatement.setInt(6,log.getPokemon());
            preparedStatement.setInt(7,log.getBugCat());
            preparedStatement.setString(8,lastTime);
            preparedStatement.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
